﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.IO;
using System.Xml.Linq;

namespace PinCity.Localization
{
    /// <summary>
    /// Class, that is used ONLY by LocalizationManager class.
    /// Its method GetString should not be used. Use LocalizationManager.GetString instead.
    /// </summary>
    public class Localization
    {
        /// <summary>
        /// Contains information about chosen culture.
        /// Gets its value in constructor.
        /// Is not used at the moment.
        /// </summary>
        private CultureInfo culture;
        /// <summary>
        /// Dictionary of localized strings, taken from corresponding localization XML file.
        /// First parameter is key, second is the localized string.
        /// </summary>
        public IDictionary<string, string> dictionary = new Dictionary<string, string>();

        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Localization));

        /// <summary>
        /// Gets value of chosen culture for this instance of Localization.
        /// </summary>
        public CultureInfo Culture
        {
            get { return this.culture; }
            private set { this.culture = value; }
        }

        /// <summary>
        /// Constructor for Localization class. Fills Dictionary and initializes Culture field.
        /// </summary>
        /// <param name="source">Source file stream, from which Localization gets values for its dictionary.</param>
        /// <param name="culture">Used to initialize culture field.</param>
        public Localization(FileStream source, CultureInfo culture)
        {
            XDocument file = XDocument.Load(source);        //open xml file from <source>
            IEnumerable<XElement> dict = file.Root.Descendants("localizedString");      //create a dictionary, that will contain translations from keys to localizaed strings
            foreach (XElement e in dict)                                        //Fill
            {                                                                   //Da
                this.dictionary.Add(e.Attribute("key").Value, e.Value);         //Fricken
            }                                                                   //Dicshunary
            this.Culture = culture;     //sets the <Culture> filed for whatever reason
            Localization.logger.Debug(dictionary);      //Log the debug info
        }

        /// <summary>
        /// Gets localized string. Used by LocalizationManager class.
        /// To get localized string outside LocalizationManager use LocalizationManager.GetString static method instead.
        /// </summary>
        /// <param name="key">Key of returning string. Must be present in corresponding localization file.</param>
        /// <returns>Returns localized string. If no key found in corresponding localization XML file returns key itself.</returns>
        public string GetString(string key)
        {
            string result = key.ToUpper();
            if (this.dictionary.Keys.Contains(key))
                result = this.dictionary[key];
            else Localization.logger.Error("Localization for " + Culture.Name + " does not contain value for " + key);
            return result;
        }
    }
}